//
// StyleTabCtrl.cpp : Version 1.0 - see article at CodeProject.com
//
// Author:  Darren Sessions
//          
//
// Description:
//     StyleTabCtrl is a CEdit derived control that uses the Style class
//	   and GDI+ to support different styles
//
// History
//		Version 1.1 - 2008 July 25
//		- Fixed incorrect string in OnNcPaint during some repaint operations
//
//		Version 1.0 - 2008 June 20
//		- Initial public release
//
// License:
//     This software is released under the Code Project Open License (CPOL),
//     which may be found here:  http://www.codeproject.com/info/eula.aspx
//     You are free to use this software in any way you like, except that you 
//     may not sell this source code.
//
//     This software is provided "as is" with no expressed or implied warranty.
//     I accept no liability for any damage or loss of business that this 
//     software may cause.
//
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StyleTabCtrl.h"

#include "colors.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// StyleTabCtrl

StyleTabCtrl::StyleTabCtrl()
	:m_EditRect(0,0,0,0)
{
	m_OffsetY = -1;
	m_HollowBrush.CreateStockObject(HOLLOW_BRUSH);
	m_TextColor = Black;
	m_iDlgCount = 0;
}

StyleTabCtrl::~StyleTabCtrl()
{
}

void StyleTabCtrl::SetStack(Stack stack)
{
	Rect r(stack.Frame.frect);
	CRect cr(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
	m_EditRect = cr;
	m_Style.AddStack(stack);
}
void StyleTabCtrl::SetTextClr(COLORREF clr)
{
	m_TextColor = clr;
}

BEGIN_MESSAGE_MAP(StyleTabCtrl, CTabCtrl)
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_ERASEBKGND()
	ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
	ON_WM_NCPAINT()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// StyleTabCtrl message handlers

HBRUSH StyleTabCtrl::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(m_TextColor);
    return (HBRUSH)m_HollowBrush;
}

BOOL StyleTabCtrl::OnEraseBkgnd(CDC* pDC) 
{
	m_Style.SetPosition(0, m_OffsetY);
	m_Style.PaintStyle(pDC, m_EditRect);
	return TRUE;
}

void StyleTabCtrl::OnUpdate() 
{
	Invalidate(); 
}

void StyleTabCtrl::OnNcPaint()
{
	Default();

	CWindowDC dc(this);

	CString str;
	GetWindowText(str);
	m_Style.SetPosition(0, 0);
	m_Style.PaintStyle(&dc, m_EditRect);
	SetWindowText(str);
}


void StyleTabCtrl::HideBorderAndTab(void)
{
	GetClientRect(m_VisibleRect);
	m_VisibleRect.DeflateRect(2,2);
	m_VisibleRect.top += 25;
	GetParent()->ClientToScreen(m_VisibleRect);
	HRGN hRgn = CreateRectRgnIndirect(&m_VisibleRect);
	SetWindowRgn(hRgn, TRUE);
	DeleteObject(hRgn);
	return;
}

void StyleTabCtrl::AddDialog(CDialog& dlg, LPSTR sText)
{
	TCITEM item;
	item.mask = TCIF_TEXT;


	item.pszText = "";
	if(sText != NULL)
		item.pszText = sText;

	InsertItem (m_iDlgCount++,&item);

	dlg.SetWindowPos(NULL,m_VisibleRect.left, m_VisibleRect.top, m_VisibleRect.Width(), m_VisibleRect.Height(),SWP_SHOWWINDOW);
	dlg.ShowWindow(SW_HIDE);
	//RedrawWindow();
	return;
}
